package com.hitsz.cdq;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.hitsz.cdq.MainActivity.IP_ADDRESS_SERVER;
import static com.hitsz.cdq.MainActivity.PASSWORD;
import static com.hitsz.cdq.MainActivity.USER;

/**
 * Created by ChenDongqiang on 2017/6/26.
 */
public class CheAddFragment extends Fragment {
    private Connection mysql_conn;
    private String sql;
    private String che_id = null;
    private Button btn_che_add_commit;

    private EditText edit_che_add_id, edit_che_add_name, edit_che_add_ip, edit_che_add_port, edit_che_add_brief;
    private TextView tv_che_add_state;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_che_add, container, false);

        edit_che_add_id = (EditText) view.findViewById(R.id.edit_che_add_id);
        edit_che_add_name = (EditText) view.findViewById(R.id.edit_che_add_name);
        edit_che_add_ip = (EditText) view.findViewById(R.id.edit_che_add_ip);
        edit_che_add_port = (EditText) view.findViewById(R.id.edit_che_add_port);
        edit_che_add_brief = (EditText) view.findViewById(R.id.edit_che_add_brief);

        tv_che_add_state = (TextView) view.findViewById(R.id.tv_che_add_state);

        btn_che_add_commit = (Button) view.findViewById(R.id.btn_che_add_commit);
        btn_che_add_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                che_id = edit_che_add_id.getText().toString();
                String info = edit_che_add_brief.getText().toString();
                String name = edit_che_add_name.getText().toString();
                String type_che = "A";
                String state = "0";

                if (!che_id.equals("")) {
                    select_che_liang_info();

                    sql = "INSERT INTO che_liang_info(id, name, type, info, state,update_time) VALUES ('"
                            + che_id + "','" + name + "', '" + type_che + "', '" + info + "'," + state + ",'" + get_current_time() + "')";
                    insert_che_liang_info();

                    System.out.println("result=" + return_result);
                } else {
                    tv_che_add_state.setText("添加失败，未填写小车编号");
                    tv_che_add_state.setTextColor(Color.RED);
                }
            }
        });
        return view;
    }

    private String get_current_time() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        return time;
    }


    public void insert_che_liang_info() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.i("onConn", "onConn");
                Statement statement = null;
                boolean result;
                try {
                    mysql_conn = MySqlUtil.openConnection("jdbc:mysql://" + IP_ADDRESS_SERVER + "/smart_car", USER, PASSWORD);
                    if (mysql_conn == null) {
                        Log.i("onConn", "服务器数据库连接失败");
                        return;
                    }
                    statement = mysql_conn.createStatement();
                    result = statement.execute(sql);
                    System.out.println("execute_insert result:" + result);
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (statement != null) {
                            statement.close();
                        }
                    } catch (SQLException sql_e) {
                        sql_e.printStackTrace();
                    }
                }
            }
        }
        ).start();

    }

    Boolean return_result = false;

    //查询车辆信息数据表中是否存在id对应的那行数据
    public void select_che_liang_info() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                Statement statement = null;
                ResultSet result = null;
                String sql = "SELECT * FROM che_liang_info WHERE id=" + che_id;
                try {
                    mysql_conn = MySqlUtil.openConnection("jdbc:mysql://" + IP_ADDRESS_SERVER + "/smart_car", USER, PASSWORD);
                    if (mysql_conn == null) {
                        Log.i("onConn", "服务器数据库连接失败");
                    }
                    statement = mysql_conn.createStatement();
                    result = statement.executeQuery(sql);
                    switch (String.valueOf(result.first())) {
                        case "false":
                            return_result = false;
                        case "true":
                            return_result = true;
                    }
                    System.out.println(result.first());
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (result != null) {
                            result.close();
                        }
                        if (statement != null) {
                            statement.close();
                        }
                    } catch (SQLException sql_e) {
                        sql_e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}
